<?php

namespace App\Http\Controllers\Api;

use App\Models\Branch;
use App\Models\Grade;
use App\Models\Member;
use App\Models\Program;
use App\Models\Task;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class BranchController extends Controller
{
    public function findBranch(){
        return response()->json(Branch::all());
    }
    public function task(Request $request){
        if($request->user=='ming'||$request->user=='admin'){
            $sum = Program::where('plan','B')->count();
            return response()->json(['sum'=>$sum]);
        }else{
            $member=Member::where('username',$request->user)->first();
            $sum=Task::where('branch_id',$member->branch_id)->where('state',0)->count();
            return response()->json(['sum'=>$sum]);
        }
    }
    public function score(Request $request){
        if($request->user=='admin'){
            return response()->json(['sum'=>0]);
        }
        $member=Member::where('username',$request->user)->first();
        if($member->branch_id==3||$member->branch_id==4||$member->branch_id==5||$member->branch_id==6){

            if($request->one==1&&$member->branch_id==5){
                $sum=Grade::where('state','<',2)->where('plan',1)->count();
                return response()->json(['sum'=>$sum]);
            }else{
                $sum=Grade::where('state',0)->where('branch_id',$member->branch_id)->where('plan',1)->count();
                return response()->json(['sum'=>$sum]);
            }
        }
        return response()->json(['sum'=>'']);
    }
}
